/*! ----------------------------------------------------------------------------
 * @file	port.h
 * @brief	HW specific definitions and functions for portability
 *
 * @attention
 *
 * Copyright 2013 (c) DecaWave Ltd, Dublin, Ireland.
 *
 * All rights reserved.
 *
 * @author DecaWave
 */


#ifndef PORT_H_
#define PORT_H_

#ifdef __cplusplus
extern "C" {
#endif

#include "stm32f4xx_hal.h"
#include "stm32f4xx.h"
#include "stm32f4xx_hal_gpio.h"
#include "deca_device_api.h"

extern int writetospi_serial(uint16 headerLength,
                             const uint8 *headerBuffer,
                             uint32 bodylength,
                             const uint8 *bodyBuffer);

extern int readfromspi_serial(uint16	headerLength,
                              const uint8 *headerBuffer,
                              uint32 readlength,
                              uint8 *readBuffer );

#define writetospi  writetospi_serial
#define readfromspi readfromspi_serial

#define SPIx_PRESCALER				SPI_BAUDRATEPRESCALER_64

#define SPIx_Handler			hspi1
#define SPIx							SPI1
#define SPIx_GPIO					GPIOB
#define SPIx_CS						0x8000//GPIO_Pin_15
#define SPIx_CS_GPIO			GPIOA
#define SPIx_SCK					GPIO_Pin_3
#define SPIx_MISO					GPIO_Pin_4
#define SPIx_MOSI					GPIO_Pin_5
														
#define DW1000_RSTn					0x0200//GPIO_Pin_9
#define DW1000_RSTn_GPIO		GPIOB
															
#define DECAIRQ             0x0100//GPIO_Pin_8														
#define DECAIRQ_GPIO        GPIOB
#define DECAIRQ_EXTI_IRQn   EXTI9_5_IRQn


#define port_SPIx_busy_sending()		(__HAL_SPI_GET_FLAG(&SPIx_Handler, SPI_FLAG_TXE)==(RESET))
#define port_SPIx_no_data()					(__HAL_SPI_GET_FLAG(&SPIx_Handler, SPI_FLAG_RXNE)==(RESET))



#define port_GetEXT_IRQStatus()             __HAL_GPIO_EXTI_GET_IT(DECAIRQ)
#define port_DisableEXT_IRQ()               HAL_NVIC_DisableIRQ(DECAIRQ_EXTI_IRQn)
#define port_EnableEXT_IRQ()                HAL_NVIC_EnableIRQ(DECAIRQ_EXTI_IRQn);
#define portGetTickCount() 									HAL_GetTick()

void Reset_DW1000(void);
void dwm1000_peripherals_init (void);
void SPI_ChangeRate(uint16_t scalingfactor);
void SPI_ConfigFastRate(uint16_t scalingfactor);
void spi_set_rate_low (void);
void spi_set_rate_high (void);
unsigned long portGetTickCnt(void);


#ifdef __cplusplus
}
#endif

#endif /* PORT_H_ */
